package com.stoneframe.service.manageAuthorize;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stoneframe.service.BaseService;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.ProviderHelper;
import com.stoneframe.util.baseApp.ToolHelper;
import com.stoneframe.util.baseDate.DateHelper;
import com.stoneframe.util.baseGuid.GuidHelper;

@Service(value="sysAuthorizeService")
public class SysAuthorizeService extends BaseService {

	protected SysAuthorizeService() {
		super("SysAuthorizeMapper");
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * getMemberUser
	 * */
	public List<PageData> getMemberUser(String roleId) throws Exception{
		return (List<PageData>)dao.findForList(mapper+".getMemberUser", roleId);
	}
	
	/** 
	 * 获取权限菜单  
	 * */
	public List<PageData> getModulePermission(PageData pd) throws Exception{
		return (List<PageData>)dao.findForList(mapper+".getModulePermission", pd);
	}
	
	// 获取权限按钮
	public List<PageData> getModuleButtonPermission(PageData pd) throws Exception{
		return (List<PageData>)dao.findForList(mapper+".getModuleButtonPermission", pd);
	}
	
	// 获取权限视图
	public List<PageData> getModuleColumnPermission(PageData pd) throws Exception{
		return (List<PageData>)dao.findForList(mapper+".getModuleColumnPermission", pd);
	}
	
	// 获取用户菜单
	public List<PageData> getModuleList() throws Exception{
		if(ProviderHelper.isSystem()){ //是否是管理员
			return (List<PageData>)dao.findForList("SysModuleMapper.listAll", null);
		}
		//非管理员
		PageData userData=ProviderHelper.getCurrentUser();
		String userId=userData.getString("userId");
		return (List<PageData>)dao.findForList(mapper+".getModuleList", userId);
	}
	
	// 获取用户按钮
	public List<PageData> getModuleButtonList() throws Exception{
		if(ProviderHelper.isSystem()){ //是否是管理员
			return (List<PageData>)dao.findForList("SysModuleButtonMapper.listAll", null);
		}
		PageData userData=ProviderHelper.getCurrentUser();
		String userId=userData.getString("userId");
		return (List<PageData>)dao.findForList(mapper+".getModuleButtonList", userId);
	}
	
	// 获取用户视图
	public List<PageData> getModuleColumnList(String userId) throws Exception{
		return (List<PageData>)dao.findForList(mapper+".getModuleColumnList", userId);
	}
	
	// 获取数据权限
	public List<PageData> getAuthorizeDataList(PageData pd) throws Exception{
		return (List<PageData>)dao.findForList(mapper+".getAuthorizeDataList", pd);
	}
	
	@Transactional
	public void saveMember(int category,String objectId, String[] userIds) throws Exception{
		//删除对应的权限
		dao.del(mapper+".del", objectId);
		//菜单
		int sortCode=1;
		for(String userId:userIds){
			PageData userData=new PageData();
			userData.put("userRelationId", GuidHelper.getTimeUuid());
			userData.put("userId", userId);
			userData.put("category", category);
			userData.put("objectId", objectId);
			userData.put("isDefault", 0);
			userData.put("sortCode", sortCode++);
			userData.put("createDate", DateHelper.getTime());
			dao.save("SysUserRelationMapper.save", userData);
		}
	}
	
	@Transactional
	public void saveAuthorize(int category,String objectId, String[] moduleIds, String[] moduleButtonIds, 
			String[] moduleColumnIds, List<Map<String, Object>> authorizeDataList) throws Exception{
		
		//删除对应的权限
		dao.del(mapper+".del", objectId);
		
		//菜单
		int sortCode=1;
		for(String moduleId:moduleIds){
			if(ToolHelper.isEmpty(moduleId)){
				continue;
			}
			PageData moduleData=new PageData();
			moduleData.put("authorizeId", GuidHelper.getTimeUuid());
			moduleData.put("category", category);
			moduleData.put("objectId", objectId);
			moduleData.put("itemType", 1);
			moduleData.put("itemId", moduleId);
			moduleData.put("sortCode", sortCode++);
			moduleData.put("createDate", DateHelper.getTime());
			dao.save(mapper+".save", moduleData);
		}
		
		//按钮
		sortCode=1;
		for(String buttonId:moduleButtonIds){
			if(ToolHelper.isEmpty(buttonId)){
				continue;
			}
			PageData moduleData=new PageData();
			moduleData.put("authorizeId", GuidHelper.getTimeUuid());
			moduleData.put("category", category);
			moduleData.put("objectId", objectId);
			moduleData.put("itemType", 2);
			moduleData.put("itemId", buttonId);
			moduleData.put("sortCode", sortCode++);
			moduleData.put("createDate", DateHelper.getTime());
			dao.save(mapper+".save", moduleData);
		}
		
		//视图
		sortCode=1;
		for(String columnId:moduleColumnIds){
			if(ToolHelper.isEmpty(columnId)){
				continue;
			}
			PageData moduleData=new PageData();
			moduleData.put("authorizeId", GuidHelper.getTimeUuid());
			moduleData.put("category", category);
			moduleData.put("objectId", objectId);
			moduleData.put("itemType", 3);
			moduleData.put("itemId", columnId);
			moduleData.put("sortCode", sortCode++);
			moduleData.put("createDate", DateHelper.getTime());
			dao.save(mapper+".save", moduleData);
		}
		
		//数据权限
		sortCode=1;
		dao.del("SysAuthorizeDataMapper.del", objectId);
		for(Map<String, Object> authorizeMap:authorizeDataList){
			PageData authorizeData=new PageData();
			authorizeData.put("authorizeDataId", GuidHelper.getTimeUuid());
			authorizeData.put("category", category);
			authorizeData.put("objectId", objectId);
			authorizeData.put("isRead", authorizeMap.get("IsRead"));
			authorizeData.put("authorizeType", authorizeMap.get("AuthorizeType"));
			authorizeData.put("sortCode", sortCode++);
			authorizeData.put("createDate", DateHelper.getTime());
			dao.save("SysAuthorizeDataMapper.save", authorizeData);
		}
		
	}
	

}
